package com.example.administrator.bdsjws.activities;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBar;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;


import com.example.administrator.bdsjws.MyApplication;
import com.example.administrator.bdsjws.R;
import com.example.administrator.bdsjws.activities.base.BaseActivity;
import com.example.administrator.bdsjws.services.IVersionUpdate;
import com.example.administrator.bdsjws.vo.VersionInfo;

import java.io.IOException;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class Updated_popups extends BaseActivity {

    private ImageView aaa;
    @Override
    public int getContentXML() {
        return R.layout.activity_updated_popups;
    }

    @Override
    public void initData() {

    }

    @Override
    public void initView() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        aaa= (ImageView) findViewById(R.id.imageyuan);

        inite(aaa);
        iniy();

    }

    public static int getVersionCode(Context context)//获取版本号(内部识别号)
    {
        try {
            PackageInfo pi=context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            return pi.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return 0;
        }
    }
    public void iniy() {
        //检查网络状态 2无网  1 有网络，但比较慢 窗体关闭  3  ，网络正常，可以下载  4、网络正常，已经是最新版本
        boolean b=checkNet();
        if(b){
            //访问服务器使用框架 retrofit  取JSON数据  取到apkUrl  判断版本是否是最新的
            Retrofit retrofit=new Retrofit.Builder()
                    .baseUrl("http://www.babyiloveyou.ml/")
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            IVersionUpdate versionUpdate=retrofit.create(IVersionUpdate.class);//创建接口对象
            Call<VersionInfo> versionInfoCall=versionUpdate.getVersion();
            //异步处理

            versionInfoCall.enqueue(new Callback<VersionInfo>() {
                @Override
                public void onResponse(Call<VersionInfo> call, Response<VersionInfo> response) {
                    //网络正常  3  ，网络正常，可以下载  4、网络正常，已经是最新版本
                    VersionInfo versionInfo= response.body();
                    int netcode=versionInfo.getVersionCode();//最新的code
                    int oldcode=getVersionCode(MyApplication.getContext());
                    Log.d("qqqqqqq","old"+oldcode+"new"+netcode);
                    if(netcode>oldcode){//有更新
                        // setResult(3);因为要将VersionInfo 传递过去
                        Intent intent=new Intent();
                        intent.putExtra("VersionInfo",versionInfo);
                        setResult(3,intent);
                        finish();
                    }else{
                        setResult(4);
                        finish();
                    }
                }

                @Override
                public void onFailure(Call<VersionInfo> call, Throwable t) {
                    //网络比较慢  关闭窗体
                    setResult(1);
                    finish();

                }
            });


        }else{
            setResult(2);//传值
            finish();
        }
    }
    public boolean checkNet(){
        NetworkInfo.State wifiState = null;
        NetworkInfo.State mobileState = null;
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        wifiState = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
        mobileState = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
        if (wifiState != null && mobileState != null && NetworkInfo.State.CONNECTED != wifiState
                && NetworkInfo.State.CONNECTED == mobileState) {
            // 手机网络连接成功
            Toast.makeText(this, "手机网络连接成功", Toast.LENGTH_SHORT).show();
            return true;
        } else if (wifiState != null && mobileState != null
                && NetworkInfo.State.CONNECTED != wifiState
                && NetworkInfo.State.CONNECTED != mobileState) {
            // 手机没有任何的网络
            Toast.makeText(this, "手机没有任何的网络", Toast.LENGTH_SHORT).show();
            return false;
        } else if (wifiState != null && NetworkInfo.State.CONNECTED == wifiState) {
            // 无线网络连接成功
            Toast.makeText(this, "无线网络连接成功", Toast.LENGTH_SHORT).show();
            return true;
        }
        return false;


}
    public void inite(View view){

//        ScaleAnimation scaleAnimation=new ScaleAnimation(0,2f,0,2f, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
//        ScaleAnimation scaleAnimation2=new ScaleAnimation(2f,0,2f,0, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
//        scaleAnimation2.setDuration(2000);
//        scaleAnimation2.setRepeatCount(5);
//        scaleAnimation.setDuration(2000);
//        scaleAnimation.setRepeatCount(5);
//        final AnimationSet animationSet=new AnimationSet(true);
//        animationSet.addAnimation(scaleAnimation);
//        animationSet.addAnimation(scaleAnimation2);
//        findViewById(R.id.imageyuan).setAnimation(animationSet);
//        animationSet.setAnimationListener(new Animation.AnimationListener() {
//
//            @Override
//            public void onAnimationStart(Animation animation) {
//
//            }
//
//            @Override
//            public void onAnimationEnd(Animation animation) {
//
//
//
//            }
//
//            @Override
//            public void onAnimationRepeat(Animation animation) {
//
//            }
//        });

        ObjectAnimator scaleYy=ObjectAnimator.ofFloat(view,"scaleY",0,5f,0);
        ObjectAnimator scaleXx=ObjectAnimator.ofFloat(view,"scaleX",0,5f,0);

        final AnimatorSet set=new AnimatorSet();
        set.play(scaleXx).with(scaleYy);
        set.setDuration(2000);
        set.start();

        ObjectAnimator scaleYy1 = ObjectAnimator.ofFloat(findViewById(R.id.imageyuan2), "scaleY", 0, 55f, 0);
        ObjectAnimator scaleXx1 = ObjectAnimator.ofFloat(findViewById(R.id.imageyuan2), "scaleX", 0, 55f, 0);
        final AnimatorSet set1 = new AnimatorSet();
        set1.play(scaleXx1).with(scaleYy1);
        set1.setDuration(2000);
        set.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                set1.start();
            }
        });

        set1.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                set.start();
            }
        });

//        new Handler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
//
//                finish();
//            }
//        },4000);
    }
}
